package com.eye.catcher.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.entity.TCodDaDictionary;
import com.eye.catcher.mapper.TCodDaDictionaryMapper;
import com.eye.catcher.service.ITCodDaDictionaryService;
import com.eye.catcher.system.dictionary.param.DictionaryDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-18
 */
@Slf4j
@Service
public class TCodDaDictionaryServiceImpl extends ServiceImpl<TCodDaDictionaryMapper, TCodDaDictionary> implements ITCodDaDictionaryService {

    @Autowired
    private TCodDaDictionaryMapper dictionaryMapper;
    @Autowired
    private RedisUtils redisUtils;

    private final static String  dictionaryTableName = "t_cod_ada_dictionary";//主表

    @Override
    public void add(DictionaryDto dictionary){
        dictionaryMapper.saveDictionary(dictionary);
        if(!dictionaryTableName.equals(dictionary.getTableName())){
            redisUtils.hdel(dictionaryTableName,dictionary.getTableName());
        }
    }
    @Override
    public void edit(DictionaryDto dictionary) {
        dictionaryMapper.editDictionary(dictionary);
        if(!dictionaryTableName.equals(dictionary.getTableName())){
            redisUtils.hdel(dictionaryTableName,dictionary.getTableName());
        }
    }

    @Override
    public void deleteByCode(DictionaryDto dictionary) {
        dictionaryMapper.deleteByCode(dictionary);
        if(!dictionaryTableName.equals(dictionary.getTableName())){
            redisUtils.hdel(dictionaryTableName,dictionary.getTableName());
        }
    }

    @Override
    public Object getCacheDictionaryJson() {
        JSONObject jsonObject = new JSONObject();
        String key1 = "";//副表
        Object obj = null;
        //获取主表
        List<TCodDaDictionary> dictionaryList = (List<TCodDaDictionary>)this.getGeneralList(new DictionaryDto(dictionaryTableName));
        List<TCodDaDictionary> sideDictionaryList = null;
        for(TCodDaDictionary codDaDictionary : dictionaryList){
            key1 = codDaDictionary.getCode();
            obj = redisUtils.hget(dictionaryTableName, key1);
            if(null != obj){
                log.info(key1 +"->redis");
                sideDictionaryList = (List<TCodDaDictionary>)obj;
            }else {
                sideDictionaryList = (List<TCodDaDictionary>) this.getGeneralList(new DictionaryDto(key1));
                //存储
                redisUtils.hset(dictionaryTableName,key1,sideDictionaryList);
            }
            jsonObject.put(codDaDictionary.getCode(),com.alibaba.fastjson.JSON.toJSONString(sideDictionaryList));
        }
        return jsonObject;
    }

    @Override
    public Object getGeneralPageList(DictionaryDto dictionary) {
        List<TCodDaDictionary> dictionaryList = dictionaryMapper.getGeneralPageList(dictionary);
        long count = dictionaryMapper.getGeneralPageCount(dictionary);

        IPage<TCodDaDictionary> returnPage = new Page<>();
        returnPage.setRecords(dictionaryList);
        returnPage.setTotal(count);
        returnPage.setCurrent(Integer.parseInt(dictionary.getPageNum()));
        returnPage.setSize(Integer.parseInt(dictionary.getPageSize()));
        returnPage.setPages((long) Math.ceil((double) count/(double) Integer.parseInt(dictionary.getPageSize())));

        return returnPage;
    }

    @Override
    public Object getGeneralList(DictionaryDto dictionary) {

        return dictionaryMapper.getGeneralList(dictionary);
    }
}
